import { defineStore } from 'pinia'
import {getMyPermissions, selectSimulateSysMenuTree} from "@/api/module/myMenu";
import {getMyUserInfo} from "@/api/module/myUser";


export const useSimulateStore = defineStore({
  id: 'simulate',
  state: () => {
    return {
      token: '',
      userInfo: {},
      permissions: [],
      menuTree: [],
    }
  },
  actions: {
    async loadMenuTree(){
      const { data, code } = await selectSimulateSysMenuTree();
      if(code == 200) {
        this.menuTree = data;
      }
    },
    async loadUserInfo(){
      const {code ,data} = await getMyUserInfo();
      this.userInfo = data;
    },
    async loadPermissions(){
      const {code ,data} = await getMyPermissions();
      this.permissions = data;
    },
    setToken(token:string){
      this.token = token
    },
    setPermissions(permissions:[]) {
      this.permissions = permissions
    },
    delAll(){
      this.token = '';
      this.userInfo = {};
      this.permissions = [];
      this.menuTree = [];
    }
  },
  persist: {
    storage: localStorage,
    paths: ['token', 'userInfo', 'permissions', 'menuTree' ],
  }
})